package org.blogspot.t0yv0.marcalog.model

import javax.jdo.annotations._
import org.blogspot.t0yv0.marcalog.util._

@PersistenceCapable(identityType = IdentityType.APPLICATION)
class Item(theTitle  : String, 
           thePerson : Option[Person], 
           theISBN   : Option[ISBN]) extends Entity {

  theTitle match {
    case null => assert(false, "Item: title cannot be null.");
    case ""   => assert(false, "Item: title cannot be empty.");
    case _    => {}
  }

  @PrimaryKey
  @Persistent
  override var id : String =
    theISBN match {
      case Some(x) =>
        "ISBN-" + x.toString;
      case None =>
        "BCL-" + (theTitle,thePerson).hashCode.abs.toString;        
    }

  @Persistent
  var codeISBN =
    theISBN match {
      case Some(i) => i.toString;
      case None    => ""
    }

  @Persistent
  var personId : String =
    thePerson match {
      case None    => "";
      case Some(p) => p.id
    }

  @Persistent
  var title = theTitle

  @Persistent    
  var key = Util.getFirstLetter(theTitle).toInt

  def isbn =
    codeISBN match {
      case "" => None;
      case x  => Some(new ISBN(x));
    }
}
